let feedMixin = {
	methods: {
		// 点赞或者取消点赞一条动态
		// item 是调用处依据上下文得到的一个数据对象
		async clickLove(item) {
			// 判断当前登录状态
			if (!uniCloud.getCurrentUserInfo().uid) {
				//this.$refs.login.openLogin()
				return
			}
			// 动态点赞
			if (item.has_like) {
				--item.like_count;
				item.has_like = false;
				// 调用API进行更新
				const db =  uniCloud.databaseForJQL()
				const db2 = uniCloud.databaseForJQL()
				await db.collection("NeuHelper-Posts").where("_id=='" + item._id  + "'").update({'like_count':item.like_count})
				try {
					await db2.collection("NeuHelper-LikePosts").where("user_id== $cloudEnv_uid &&post_id=='"+ item._id+"'").remove()
				} catch (e) {
					uni.showModal({
						title: '提示',
						content: e.message
					})
				}
				
				uni.showToast({
					title: "取消点赞",
					icon: "success",
					duration: 1000,
				});
			} else {
				++item.like_count;
				item.has_like = true;
				// 调用API对点赞表进行更新
				const db =  uniCloud.databaseForJQL()
				const db2 = uniCloud.databaseForJQL()
				let userData = await db.collection('uni-id-users').where("_id== $cloudEnv_uid ").field('_id,nickname,avatar_file').get()
				userData = userData.data[0] // 获取当前登录用户的信息
				await db.collection("NeuHelper-Posts").where("_id=='" + item._id  + "'").update({'like_count':item.like_count})
				try {
					await db2.collection('NeuHelper-LikePosts').add({
						post_id:item._id,
						user_avatar:userData.avatar_file.url,
						user_nickname:userData.nickname 
					})
				} catch (e) {
					uni.showModal({
						title: '提示',
						content: e.message
					})
				}
				// 自己点赞自己不会推送
				if(userData._id != item.post_user_id ){
					// 将点赞消息推送到发布帖子的用户
					try {
						await db2.collection('NeuHelper-Notifications').add({
							post_id:item._id, 
							toNotify_id:item.post_user_id,
							initiator_id:userData._id,
							type:1// 点赞类型
						})
					} catch (e) {
						uni.showModal({
							title: '提示',
							content: e.message
						})
					}
				}
				uni.showToast({
					title: "点赞成功",
					icon: "success",
					duration: 1000,
				});
			}
			uni.$emit('indexLoveChange',item) //用户点赞一次后触发更新index页面
			//uni.$emit('myFeedLoveChange',item) 触发的是消息管理中的
		}
	}
}
export default feedMixin
